# reference: 20190501_逻辑回归.ipynb

import pandas as pd
import sys, re, os
import numpy as np
import matplotlib.pyplot as plt

#plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

path = "./data/graduate-admissions/Admission_Predict_Ver1.1.csv"
path1 = "./data/graduate-admissions/Admission_Predict.csv"
data_df = pd.read_csv(path)
data_df1 = pd.read_csv(path1)
data_df.index = data_df['Serial No.']
data_df1.index = data_df1['Serial No.']

part_data_df = data_df[['GRE Score', 'TOEFL Score', 'University Rating', 'SOP',
         'LOR ', 'CGPA', 'Research', 'Chance of Admit ']]
part_data_df1 = data_df1[['GRE Score', 'TOEFL Score', 'University Rating', 'SOP',
         'LOR ', 'CGPA', 'Research', 'Chance of Admit ']]

combine_data_df = pd.concat([part_data_df, part_data_df1], axis=0)
combine_data_df.head(5)
combine_data_df.describe()
combine_data_df.corr()

import seaborn as sns
fig, axes = plt.subplots(figsize = (7, 6))
sns.heatmap(combine_data_df.corr(), ax=axes, annot=True,fmt= '.2f', linewidths=0.03, cmap="magma")
plt.savefig("4.4-studentScore.png") #图4.4 各个变量两两之间的相关性

#图4.6 ROC曲线和AUC值图
from sklearn.model_selection import train_test_split
train_df, test_df = train_test_split(combine_data_df, test_size=0.35, random_state=42)
train_value = train_df[[i for i in train_df.columns if i != 'Chance of Admit ']]
train_label = train_df['Chance of Admit ']
test_value = test_df[[i for i in test_df.columns if i != 'Chance of Admit ']]
test_label = test_df['Chance of Admit ']
print(train_value.shape, test_value.shape)

from sklearn.preprocessing import MinMaxScaler
norm_trans = MinMaxScaler(feature_range=(0, 1))
train_value = train_value.astype('float64')
test_value = test_value.astype('float64')
train_value_norm = norm_trans.fit_transform(train_value)
test_value_norm = norm_trans.fit_transform(test_value)
train_label = np.array([1 if i >= 0.75 else 0 for i in train_label.values])
test_label = np.array([1 if i >= 0.75 else 0 for i in test_label.values])

from sklearn.linear_model import LogisticRegression
train_x = train_value_norm
train_y = train_label.reshape(-1,1)
test_x = test_value_norm
test_y = test_label.reshape(-1,1)

logistic_fit = LogisticRegression()
logistic_fit.fit(X=train_x, y=train_y) #compile error

pred_test_y = logistic_fit.predict(test_x)

from sklearn.metrics import confusion_matrix
conf_matrix = confusion_matrix(pred_test_y, test_y)
fig, axes = plt.subplots(figsize =(5,5))
sns.heatmap(conf_matrix, linewidths=0.2, annot = True, fmt = ".0f",ax=axes)
plt.savefig("4.5-confMatrix.png")

from sklearn.metrics import roc_curve, roc_auc_score
fpr, tpr, thresholds = roc_curve(pred_test_y, test_y, pos_label = 1)
train_probs = logistic_fit.predict_proba(test_x)[:,1]
test_probs = logistic_fit.predict_proba(test_x)[:,1]

#计算AUC
auc_test = roc_auc_score(test_y, test_probs)

# 计算ROC曲线
fpr, tpr, thresholds = roc_curve(test_y, test_probs, pos_label=1)

plt.plot(fpr, tpr, color='red')
plt.plot([0, 1], [0,1], color='navy', linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.08])
plt.xlabel("FPR")
plt.ylabel("TPR")
#plt.annotate(xy=(.4, .2),xytext=(.5,.2) ,s='ROC curve (area = %0.2f)' % auc_test)
plt.savefig("4.6-rocresult.png")